package com.mitake.task;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import com.mitake.parse.MitakeQuote;
import com.mitake.parse.Stock;

public class QuoteCompare
{
	public static ArrayList<Stock[][]> store = new ArrayList<Stock[][]>();
	
	public static void compare(TelegramGroup telegramGroup)
	{
		Stock[][] stockList = new Stock[telegramGroup.stock.length][telegramGroup.count];
		/**
		 * 先将每个机房的资料解开,并同股票放在阵列内
		 */	
		for(int i = 0 ; i < telegramGroup.count ; i++)
		{
			Stock[] stocks = null;
			
			try
			{
				stocks = MitakeQuote.parse(new String(telegramGroup.httpData[i].b, "utf-8"));
			}
			catch(UnsupportedEncodingException e)
			{
				e.printStackTrace();
			}
			
			if(stocks != null)
			{
				/**
				 * 先排列成要比對陣列順序,以利等一下比對
				 */
				boolean exist = false;
				
				for(int j = 0 ; j < telegramGroup.stock.length ; j++)
				{
					exist = false;
					
					for(int k = 0 ; k < stocks.length ; k++)
					{
						if(telegramGroup.stock[j].equals(stocks[k].id))
						{
							stocks[k].host = telegramGroup.httpData[i].name + "_" + telegramGroup.httpData[i].host;
							stocks[k].responseTime = String.valueOf(telegramGroup.httpData[i].responseTime - telegramGroup.httpData[i].sendTime) + "ms";
							
							stockList[j][i] = stocks[k];
							exist = true;
							break;
						}
					}
					/**
					 * 有缺少筆數,就多增一筆進去
					 */
					if(!exist)
					{
						stockList[j][i] = new Stock();
						stockList[j][i].host = telegramGroup.httpData[i].name + "_" + telegramGroup.httpData[i].host;
						stockList[j][i].responseTime = String.valueOf(telegramGroup.httpData[i].responseTime - telegramGroup.httpData[i].sendTime) + "ms";
						stockList[j][i].status = "no_data";
						stockList[j][i].id = telegramGroup.stock[j];
					}
				}
			}
			else
			{
				/**
				 * 表示某一機房無回傳任何股票值
				 */
				for(int k = 0 ; k < telegramGroup.stock.length ; k++)
				{
					stockList[k][i] = new Stock();
					stockList[k][i].host = telegramGroup.httpData[i].name + "_" + telegramGroup.httpData[i].host;
					stockList[k][i].responseTime = String.valueOf(telegramGroup.httpData[i].responseTime - telegramGroup.httpData[i].sendTime) + "ms";
					stockList[k][i].status = "no_data";
					stockList[k][i].id = telegramGroup.stock[k];
				}
			}
		}
		/**
		 * 再来比对每个栏位,并将一样股票一起比
		 */
		for(int i = 0 ; i < stockList.length ; i++)
		{
			for(int j = 0, len = stockList[i].length - 1 ; j < len ; j++)
			{
				if(hasDifference(stockList[i][j].status, stockList[i][j + 1].status))
				{
					stockList[i][0].status += "@";
				}
				
				if(hasDifference(stockList[i][j].datetime, stockList[i][j + 1].datetime))
				{
					stockList[i][0].datetime += "@";
				}
				
				if(hasDifference(stockList[i][j].market, stockList[i][j + 1].market))
				{
					stockList[i][0].market += "@";
				}
				
				if(hasDifference(stockList[i][j].subtype, stockList[i][j + 1].subtype))
				{
					stockList[i][0].subtype += "@";
				}
				
				if(hasDifference(stockList[i][j].lastPrice, stockList[i][j + 1].lastPrice))
				{
					stockList[i][0].lastPrice += "@";
				}
				
				if(hasDifference(stockList[i][j].highPrice, stockList[i][j + 1].highPrice))
				{
					stockList[i][0].highPrice += "@";
				}
				
				if(hasDifference(stockList[i][j].lowPrice, stockList[i][j + 1].lowPrice))
				{
					stockList[i][0].lowPrice += "@";
				}

				if(hasDifference(stockList[i][j].openPrice, stockList[i][j + 1].openPrice))
				{
					stockList[i][0].openPrice += "@";
				}
				
				if(hasDifference(stockList[i][j].preClosePrice, stockList[i][j + 1].preClosePrice))
				{
					stockList[i][0].preClosePrice += "@";
				}
				
				if(hasDifference(stockList[i][j].changeRate, stockList[i][j + 1].changeRate))
				{
					stockList[i][0].changeRate += "@";
				}
				
				if(hasDifference(stockList[i][j].volume, stockList[i][j + 1].volume))
				{
					stockList[i][0].volume += "@";
				}
				
				if(hasDifference(stockList[i][j].nowVolume, stockList[i][j + 1].nowVolume))
				{
					stockList[i][0].nowVolume += "@";
				}
				
				if(hasDifference(stockList[i][j].turnoverRate, stockList[i][j + 1].turnoverRate))
				{
					stockList[i][0].turnoverRate += "@";
				}

				if(hasDifference(stockList[i][j].limitUP, stockList[i][j + 1].limitUP))
				{
					stockList[i][0].limitUP += "@";
				}
				
				if(hasDifference(stockList[i][j].limitDown, stockList[i][j + 1].limitDown))
				{
					stockList[i][0].limitDown += "@";
				}
				
				if(hasDifference(stockList[i][j].averageValue, stockList[i][j + 1].averageValue))
				{
					stockList[i][0].averageValue += "@";
				}
				
				if(hasDifference(stockList[i][j].change, stockList[i][j + 1].change))
				{
					stockList[i][0].change += "@";
				}
				
				if(hasDifference(stockList[i][j].amount, stockList[i][j + 1].amount))
				{
					stockList[i][0].amount += "@";
				}
				
				if(hasDifference(stockList[i][j].volumeRatio, stockList[i][j + 1].volumeRatio))
				{
					stockList[i][0].volumeRatio += "@";
				}
				
				if(hasDifference(stockList[i][j].buyPrice, stockList[i][j + 1].buyPrice))
				{
					stockList[i][0].buyPrice += "@";
				}
				
				if(hasDifference(stockList[i][j].sellPrice, stockList[i][j + 1].sellPrice))
				{
					stockList[i][0].sellPrice += "@";
				}

				if(hasDifference(stockList[i][j].buyVolume, stockList[i][j + 1].buyVolume))
				{
					stockList[i][0].buyVolume += "@";
				}
				
				if(hasDifference(stockList[i][j].sellVolume, stockList[i][j + 1].sellVolume))
				{
					stockList[i][0].sellVolume += "@";
				}
				
				if(hasDifference(stockList[i][j].totalValue, stockList[i][j + 1].totalValue))
				{
					stockList[i][0].totalValue += "@";
				}
				
				if(hasDifference(stockList[i][j].flowValue, stockList[i][j + 1].flowValue))
				{
					stockList[i][0].flowValue += "@";
				}

				if(hasDifference(stockList[i][j].netAsset, stockList[i][j + 1].netAsset))
				{
					stockList[i][0].netAsset += "@";
				}
				
				if(hasDifference(stockList[i][j].pe, stockList[i][j + 1].pe))
				{
					stockList[i][0].pe += "@";
				}
				
				if(hasDifference(stockList[i][j].roe, stockList[i][j + 1].roe))
				{
					stockList[i][0].roe += "@";
				}
				
				if(hasDifference(stockList[i][j].capitalization, stockList[i][j + 1].capitalization))
				{
					stockList[i][0].capitalization += "@";
				}
				
				if(hasDifference(stockList[i][j].circulatingShares, stockList[i][j + 1].circulatingShares))
				{
					stockList[i][0].circulatingShares += "@";
				}

				if(hasDifference(stockList[i][j].buyPricesTmp, stockList[i][j + 1].buyPricesTmp))
				{
					stockList[i][0].buyPricesTmp += "@";
				}
				
				if(hasDifference(stockList[i][j].sellPricesTmp, stockList[i][j + 1].sellPricesTmp))
				{
					stockList[i][0].sellPricesTmp += "@";
				}
				
				if(hasDifference(stockList[i][j].buyVolumesTmp, stockList[i][j + 1].buyVolumesTmp))
				{
					stockList[i][0].buyVolumesTmp += "@";
				}
				
				if(hasDifference(stockList[i][j].buySingleVolumesTmp, stockList[i][j + 1].buySingleVolumesTmp))
				{
					stockList[i][0].buySingleVolumesTmp += "@";
				}
				
				if(hasDifference(stockList[i][j].sellVolumesTmp, stockList[i][j + 1].sellVolumesTmp))
				{
					stockList[i][0].sellVolumesTmp += "@";
				}
				
				if(hasDifference(stockList[i][j].sellSingleVolumesTmp, stockList[i][j + 1].sellSingleVolumesTmp))
				{
					stockList[i][0].sellSingleVolumesTmp += "@";
				}
			    
			    if(hasDifference(stockList[i][j].amplitudeRate, stockList[i][j + 1].amplitudeRate))
				{
					stockList[i][0].amplitudeRate += "@";
				}
			    
			    if(hasDifference(stockList[i][j].receipts, stockList[i][j + 1].receipts))
				{
					stockList[i][0].receipts += "@";
				}
			}

			store.add(stockList);
		}
	}
	
	private static boolean hasDifference(String s1, String s2)
	{
		if(s1 != null && s2 != null && s1.equals(s2))
		{
			return false;
		}
		
		return true;
	}
}
